﻿package Cain.googleMap 
{
	import Cain.googleMap.control.zoomControl.GoogleZoomControl;
	import Cain.googleMap.marker.GoogleMarker;
	import com.google.maps.controls.ControlPosition;
	import com.google.maps.controls.ZoomControl;
	import com.google.maps.LatLng;
	import com.google.maps.Map;
	import com.google.maps.MapEvent;
	import com.google.maps.MapType;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import gs.TweenMax;
	/**
	 * 使用google地圖 V 1.0
	 * @author Cain
	 */
	public class GoogleMap extends Sprite
	{
		private var map:Map;
		private var xx:Number = 0;
		private var yy:Number = 0;
		private var size:int = 1;
		/**
		 * Map物件
		 */
		public var map_ob:Object;
		public function GoogleMap() 
		{
			
		}
		/**
		 * 
		 * 地圖初始化
		 * @param	key 金鑰
		 * @param	_ww 寬
		 * @param	_hh 高
		 * @param	_xx 經度
		 * @param	_yy 緯度
		 * @param	_size 縮放比
		 */
		public function setup(key:String = "",_ww:Number = 200,_hh:Number = 200,_xx:Number = 0,_yy:Number = 0,_size:int = 14):void 
		{
			xx = _xx;
			yy = _yy;
			size = _size;
			var map:Map = new Map();
			map.sensor = "false";
			map.key = key;
			map.setSize(new Point(_ww, _hh));
			map.addEventListener(MapEvent.MAP_READY, onMapReady);
			addChild(map);
		}
		private function onMapReady(e:MapEvent):void 
		{
			map_ob = e.currentTarget;
			e.currentTarget.setCenter(new LatLng(xx, yy), size, MapType.NORMAL_MAP_TYPE);
			e.currentTarget.enableContinuousZoom();
			dispatchEvent(new Event(MapEvent.MAP_READY));
		}
		/**
		 * 設定中心點
		 * @param	_xx 經度
		 * @param	_yy 緯度
		 */
		public function setPoint(_xx:Number,_yy:Number,_size:Number = -1):void 
		{
			xx = _xx;
			yy = _yy;
			if (_size == -1) 
			{
				_size = map_ob.getZoom();
			}
			map_ob.setCenter(new LatLng(_xx, _yy ),_size);
		}
		/**
		 * 新增google預設縮放Bar
		 */
		public function addZoomControl():void 
		{
			var controlPosition:ControlPosition = new ControlPosition(ControlPosition.ANCHOR_BOTTOM_LEFT,20,50);
			var zooControl:ZoomControl = new ZoomControl();
			zooControl.setControlPosition(controlPosition);
			map_ob.addControl(zooControl);
		}
		/**
		 * 新增google自設縮放Bar
		 * @param	_zooIn 放大物件
		 * @param	_zooOut 縮小物件
		 * @param	_zooBar 縮放捲軸拉霸
		 * @param	top 最高點
		 * @param	foot 最低點
		 */
		public function setMyZoomControl(_zooIn:DisplayObject = null,_zooOut:DisplayObject = null,_zooBar:DisplayObject = null,top:Number=0,foot:Number=0):void 
		{
			var googleZoomControl:GoogleZoomControl = new GoogleZoomControl(map_ob, _zooIn,_zooOut,_zooBar,top,foot);
		}
		/**
		 * 新增標記
		 * @param	marker 地圖標記
		 */
		public function addMarker(marker:GoogleMarker = null):void 
		{
			if (marker == null) 
			{
				var _marker:GoogleMarker = new GoogleMarker(xx, yy);
				map_ob.addOverlay(_marker);
			}else 
			{
				map_ob.addOverlay(marker);
			}
		}
		/**
		 * 新增標記陣列
		 * @param	_arr 地圖標記陣列
		 */
		public function addMarkArr(_arr:Array):void 
		{
			for each(var _marker:GoogleMarker in _arr)
			{
				map_ob.addMarker(_marker);
			}
		}
	}

}